package com.wanmait.translate.system.service.impl;

import com.wanmait.translate.dto.ResultData;
import com.wanmait.translate.pojo.SystemDict;
import com.wanmait.translate.pojo.SystemMenu;
import com.wanmait.translate.system.mapper.SystemDictMapper;
import com.wanmait.translate.system.mapper.SystemMenuMapper;
import com.wanmait.translate.system.service.SystemMenurService;
import com.wanmait.translate.utils.CommonMessage;
import com.wanmait.translate.utils.DictMessage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class SystemMenuServiceImpl implements SystemMenurService {
    @Resource
    private SystemMenuMapper systemMenuMapper;
    @Resource
    private SystemDictMapper systemDictMapper;
    /*
     * @Author huweibin
     * @Description 查询指定条件的所有菜单，同时查找字典中的相关信息放入
     * @Date 22:35 2020/11/14
     **/
    @Override
    public List<SystemMenu> list(String name) {
        List<SystemMenu> list = systemMenuMapper.list(name);
        //设连接方式
        List<SystemDict> byDictTypeKey = systemDictMapper.getByDictTypeKey(DictMessage.DICT_URL_TYPE);
        HashMap<Integer, String> map = new HashMap<>();
        for (SystemDict systemDict : byDictTypeKey) {
            map.put(systemDict.getId(),systemDict.getDictValue());
        }

        for (SystemMenu systemMenu : list) {

            systemMenu.setTargetValue(map.get(systemMenu.getTarget()));
        }
        return list;
    }

    @Override
    public SystemMenu getSelfAndParentByPrimaryKey(Integer id) {
        SystemMenu menu=systemMenuMapper.getByPrimaryKey(id);
        if (menu.getParentId()==null||menu.getParentId()==0){
            SystemMenu  parent=new SystemMenu();
            parent.setId(0);
            parent.setName(CommonMessage.ROOT);
            menu.setParent(parent);
        }else{
            SystemMenu  parent=systemMenuMapper.getByPrimaryKey(menu.getParentId());
            menu.setParent(parent);
        }
        return menu;
    }

    @Override
    public void save(SystemMenu menu) {
        systemMenuMapper.save(menu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SystemMenu menu) {
        //更新之前的对象
        SystemMenu before = systemMenuMapper.getByPrimaryKey(menu.getId());
        //获得原先所有的子节点,更新所有子节点的父节点
        List<Integer> childMenu = systemMenuMapper.getChildMenu(menu.getId());
        SystemMenu systemMenu = new SystemMenu();
        for (Integer id : childMenu) {
            //设置原先的父节点为子节点的父节点
            systemMenu.setParentId(before.getParentId());
            systemMenu.setId(id);
            systemMenuMapper.update(systemMenu);
        }
        systemMenuMapper.update(menu);
    }

    @Override
    public ResultData remove(Integer id) {
       List<Integer> list= systemMenuMapper.getChildMenu(id);
       if (list.size()==0){
           systemMenuMapper.remove(id);
           return ResultData.successsWithOutData();
       }else{
           return ResultData.fail("删除父菜单前，必须清空子菜单");
       }
    }

    public SystemMenu getByPrimaryKey(Integer id) {
        if (id==0){
            SystemMenu  menu=new SystemMenu();
            menu.setId(0);
            menu.setName(CommonMessage.ROOT);
            return menu;
        }else{
           return systemMenuMapper.getByPrimaryKey(id);
        }
    }
}
